using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Toenda.Rails.System;
using Toenda.Rails.DataContainer;

public partial class Controls_Main_ctrl_pathway : System.Web.UI.UserControl {
	private AccountProvider ap = new AccountProvider();
	private ImageProvider ip = new ImageProvider();

	/// <summary>
	/// Page_Load
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	protected void Page_Load(object sender, EventArgs e) {
		ImageItem item = null;
		Account acc = new Account();
		string username = "";
		string imagename = "";
		bool imageExist = true;

		if(Request.QueryString.Count > 0) {
			switch(Request.QueryString.GetKey(0)) {
				case "id":
					if(Session.Count > 0 && ap.CheckSessionExist(Session["Zappr"].ToString())) {
						acc = ap.GetAccountBySession(Session["Zappr"].ToString());
						username = acc.Username;
					}
					break;

				case "user":
					username = Request.QueryString.Get("user").ToString();
					acc = ap.GetAccountByUsername(username);
					break;

				case "item":
					if(ip.CheckImageExists(Request.QueryString.Get("item"))) {
						item = ip.GetImage(Convert.ToInt64(Request.QueryString.Get("item")));
						acc = ap.GetAccount(item.UserID);
						username = acc.Username;
					}
					else {
						imageExist = false;
					}
					break;

				default:
					if(Request.QueryString.Get("item") != null) {
						item = ip.GetImage(Convert.ToInt64(Request.QueryString.Get("item")));
						acc = ap.GetAccount(item.UserID);
						username = acc.Username;
					}
					break;
			}
		}

		if(imageExist) {
			if(Request.QueryString.Get("item") != null) {
				item = ip.GetImage(Convert.ToInt64(Request.QueryString.Get("item")));
				imagename = item.Name;
			}

			// avatar
			HyperLink av = new HyperLink();
			av.CssClass = "avatarImg";
			//av.NavigateUrl = "~/default.aspx?id=images&user=" + username;
			av.NavigateUrl = "~/images/" + username + "/default.aspx";

			if(acc.Avatar != "" && acc.Avatar != null) {
				av.ImageUrl = "~/Avatar/" + acc.Avatar;
			}
			else {
				av.ImageUrl = "~/App_Themes/Default/Images/avatar_placeholder.jpg";
			}

			pathWayAvatar.Controls.Add(av);
			pathWayAvatar.Controls.Add(new LiteralControl(""));

			// first slash
			pathWayText.Controls.Add(new LiteralControl("/"));

			// link dashboard
			HyperLink linkDash = new HyperLink();
			linkDash.CssClass = "pathWayLink";
			linkDash.Text = GetLocalResourceObject("Photos").ToString();
			//linkDash.NavigateUrl = "~/default.aspx?id=explore";
			linkDash.NavigateUrl = "~/explore/default.aspx";

			pathWayText.Controls.Add(linkDash);

			// second slash
			pathWayText.Controls.Add(new LiteralControl("/"));

			// link user
			HyperLink linkUser = new HyperLink();
			linkUser.CssClass = "pathWayLink";
			linkUser.Text = username;
			//linkUser.NavigateUrl = "~/default.aspx?id=images&user=" + username;
			linkUser.NavigateUrl = "~/images/" + username + "/default.aspx";

			pathWayText.Controls.Add(linkUser);

			string imgtitle = imagename;

			if(imgtitle.Length > 32) {
				if(imgtitle.IndexOf(' ') == -1 || imgtitle.IndexOf(' ') > 31) {
					imgtitle = imagename.Substring(0, 30) + "...";
				}
			}

			if(imgtitle != "") {
				// third slash
				pathWayText.Controls.Add(new LiteralControl("/"));

				// link user
				Label lblImage = new Label();
				lblImage.CssClass = "pathWayText";
				lblImage.Text = imgtitle;

				pathWayText.Controls.Add(lblImage);
			}
		}
	}
}
